package company_dancecode;

import java.util.Scanner;

public class T1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int round = sc.nextInt();
        for (int i = 0; i < round; i++) {
            int n = sc.nextInt();
            int[] point = new int[n];
            int[] award = new int[n];
            for (int j = 0; j < n; j++) {
                point[j] = sc.nextInt();
            }
            for (int j = 0; j < n; j++) {
                if (award[j] == 0) {
                    getCurrIndexAwardC(j, n, point, award);
                }
            }
            int sum = 0;
            for (int i1 : award) {
                sum += i1;
            }
            System.out.println(sum);
        }
    }

    public static int getCurrIndexAward(int index, int n, int[] point, int[] award) {
        if (award[index] != 0) {
            return award[index];
        }
        int left = index - 1;
        int right = index + 1;
        if (left < 0) {
            left = left + n;
        }
        if (right >= n) {
            right = right - n;
        }
        if (point[left] >= point[index] && point[right] >= point[index]) {
            award[index] = 1;
            return 1;
        }
        if (point[left] < point[index]) {
            int left_awards = award[left];
            if (award[left] == 0) {
                left_awards = getCurrIndexAward(left, n, point, award);
            }
            award[index] = left_awards + 1;
        }
        if (point[right] < point[index]) {
            int right_awards = award[right];
            if (award[right] == 0) {
                right_awards = getCurrIndexAward(right, n, point, award);
            }
            if (award[index] < right_awards + 1) {
                award[index] = right_awards + 1;
            }
        }
        return award[index];
    }

    public static int getCurrIndexAwardC(int index, int n, int[] point, int[] award) {
        if (award[index] != 0) {
            return award[index];
        }
        int leftB = index;
        int rightB = index;
        int left = index - 1;
        int right = index + 1;
        if (left < 0) {
            left = left + n;
        }
        if (right >= n) {
            right = right - n;
        }
        int lcount = 0;
        while (point[left] < point[leftB]) {
            lcount++;
            leftB = left;
            left--;
            if (left < 0) {
                left = left + n;
            }
        }
        int rcount = 0;
        while (point[right] < point[rightB]) {
            rcount++;
            rightB = right;
            right++;
            if (right >= n) {
                right = right - n;
            }
        }
        int count = Math.max(lcount, rcount);
        award[index] = count + 1;
        return award[index];
    }
}

